﻿// Gravity Well


var canvas = document.getElementById('gravitywell');
var context;
var width = 10;
var height = 10;
var xRange, xMin, xMax, yRange, yMin, yMax;
var lastMouseX = 0;
var lastMouseY = 0;
var pi = Math.PI;


// Check for the canvas element and get its context.
if (canvas && canvas.getContext) {
    context = canvas.getContext('2d');

    if (context) {
        Game();
    }
} else {
    document.write("There was a problem trying to acquire the canvas.<br />");
    document.write("Check that the script has been loaded after the canvas.<br />");
}

// Game class
function Game() {
    SetZoom(0, 0, 13);
    Grid();
}

function testDraw() {
    var testCanvas = document.getElementById("gravitywell");
    if (canvas.getContext) {
        document.write("Canvas supported<br />");
        var ctx = testCanvas.getContext("2d");
    } else {
        document.write("Canvas not supported<br />");
        return;
    }
    ctx.fillStyle = "rgb(200, 0, 0)";
    ctx.fillRect(10, 10, 55, 50);
    ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
    ctx.fillRect(30, 30, 55, 50);
}

// Set new position and zoom level.
function SetZoom(xMid, yMid, newXRange, newYRange) {
    xRange = newXRange;
    xMin = xMid - (xRange * 0.5);
    xMax = xMid + (xRange * 0.5);

    if (!newXRange) {
        yRange = xRange * canvas.clientHeight / canvas.clientWidth;
    } else {
        yRange = newYRange;
    }
    yMin = yMid - (yRange * 0.5);
    yMax = yMid + (yRange * 0.5);
}

// Grid class
function Grid() {
    // Set dimensions and clear canvas.
    height = canvas.height = canvas.clientHeight;
    width = canvas.width = canvas.clientWidth;

    // Compute numeric range of 14 pixels, the smallest grid square.
    var rangeMin = xRange * 14 / width;

    // Find the first round number above that range.
    var gridRange = Math.pow(10, Math.ceil(Math.log(rangeMin) / Math.LN10));

    // Reduce it by half or one fifth.
    if ((gridRange * 0.2) > rangeMin) {
        gridRange *= 0.2;
    } else if ((gridRange * 0.5) > rangeMin) {
        gridRange *= 0.5;
    }

    var lineColourOrigin = '#000000';
    var lineColourUnlabeled = '#DDE5FF';
    var lineColourLabeled = '#8888ED';

    // Setup drawing options.
    context.lineWidth = 1;
    context.textBaseline = 'bottom';

    // Draw the X grid lines.
    var index = Math.floor(xMin / gridRange);
    var xGrid = index * gridRange;
    for (; xGrid <= xMax; xGrid = index * gridRange) {
        var xPos = (((xGrid - xMin) * width) / xRange);
        if (Math.abs(index) < 1e-19) {
            // Mark the origin of the grid.
            if (xPos > 20) {
                context.fillStyle = lineColourOrigin;
                context.font = 'bold 12px sans-serif';
                //context.fillText("0.0", xPos + 2, height - 2);
            }
            context.strokeStyle = lineColourOrigin;
        } else if ((index % 5) != 0) {
            // Draw an unlabeled grid line.
            context.strokeStyle = lineColourUnlabeled;
        } else {
            // Draw and label a grid line.
            if (xPos > 20) {
                context.fillStyle = lineColourLabeled;
                context.font = 'normal 12px sans-serif';
                //context.fillText(makeLabel(xGrid), xPos + 2, height - 2);
            }
            context.strokeStyle = lineColourLabeled;
        }

        // Draw line
        context.beginPath();
        context.moveTo(xPos, 0);
        context.lineTo(xPos, height);
        context.stroke();
        context.closePath();

        ++index;
    }

    // Find the Y gridrange.
    index = Math.floor(yMin / gridRange);
    var yGrid = index * gridRange;
    for (; yGrid <= yMax; yGrid = index * gridRange) {
        var yPos = (((yMax - yGrid) * height) / yRange);
        if (Math.abs(index) < 1e-9) {
            // Mark origin of the grid.
            context.fillStyle = lineColourOrigin;
            context.font = 'bold 12px sans-serif';
            //context.fillText("0.0", 2, yPos);
            context.strokeStyle = lineColourOrigin;
        } else if ((index % 5) != 0) {
            // Draw an unlabeled grid line.
            context.strokeStyle = lineColourUnlabeled;
        } else {
            // Draw and label a grid line.
            context.fillStyle = lineColourLabeled;
            context.font = 'normal 12px sans-serif';
            //context.fillText(makeLabel(yGrid), 2, yPos);
            context.strokeStyle = lineColourLabeled;
        }

        // Draw the actual line.
        context.beginPath();
        context.moveTo(0, yPos);
        context.lineTo(width, yPos);
        context.stroke();
        context.closePath();

        ++index;
    }
}

// GravityWell class
function GravityWell() {
}


// Starfield class
function Starfield() {
}

// Player class
function Player() {
}

// Mouse class
function Mouse() {
}

// Timer class
function Timer() {
}

// Particle class
function Particle() {
}

// Particle Emitter class
function ParticleEmitter() {
}
